#include "adc_thread.h"
#include "maindefs.h"
#include "messages.h"

void init_adc_thread(adc_thread_struct *tptr)
{
	tptr->adc_buf[0] = -1;
	tptr->adc_buf[1] = -1;
	tptr->chan = -1;
}

int adc_lthread(adc_thread_struct *tptr, int msgtype, int length, unsigned char msgbuffer[])
{
	signed char retval = 0;
	int counter = 0;
	if (msgtype == MSGT_ADC) {
		if (length != 4) {
			// throw an error if the message does not have a length of three
			HANDLE_ERROR(MSGBAD_LEN);
		}
		counter = msgbuffer[0];
		// Store the digital value
		tptr->adc_buf[0] = msgbuffer[1];
		tptr->adc_buf[1] = msgbuffer[2];
		// Store the channel the value was received from
		tptr->chan = msgbuffer[3];
#if !SENSORS_PIC
		// Pass the message to I2C
		retval = FromMainHigh_sendmsg(length, MSGT_ADC, (void *) msgbuffer);
		if (retval < 0) {
			HANDLE_ERROR(retval);
		}
#endif
	return retval;
	}
}
